package org.hepeng.hope.entity

import org.hibernate.annotations.ColumnDefault
import javax.persistence.Column
import javax.persistence.ConstraintMode
import javax.persistence.Entity
import javax.persistence.FetchType.*
import javax.persistence.ForeignKey
import javax.persistence.JoinColumn
import javax.persistence.JoinTable
import javax.persistence.ManyToMany
import javax.persistence.Table
import javax.persistence.UniqueConstraint
import org.hepeng.hope.entity.Entity as E


const val ROLE_TABLE_NAME = "role"

@Entity
@Table(name = ROLE_TABLE_NAME , uniqueConstraints = [UniqueConstraint(columnNames = ["name" , "code"])])
internal data class Role(@Column(name = "`name`" , nullable = false , length = 50) var name: String,
                @Column(name = "`code`" , nullable = false , length = 200) var code: String,
                @Column(name = "`desc`" , length = 500) @ColumnDefault("''") var desc: String
) : E() {

    @ManyToMany(fetch = EAGER)
    @JoinTable(name = ROLE_AUTHORITY_TABLE_NAME ,
        joinColumns = [JoinColumn(name = "roleId")] ,
        foreignKey = ForeignKey(name= "none" , value = ConstraintMode.NO_CONSTRAINT) ,
        inverseJoinColumns = [JoinColumn(name = "authorityId")] ,
        inverseForeignKey = ForeignKey(name= "none" , value = ConstraintMode.NO_CONSTRAINT)
    )
    lateinit var authoritys: Set<Authority>

}